﻿/*
 *	StringUtil
 *	@author	kakera
 *	@version	1.5
 *	@since	
 */
class StringUtil {
	function StringUtil() {
	}
	/**
	 *	替换字符串
	 *	
	 *	@usage	
	 *	@param	str	原始字符串
	 *	@param	find	替换前
	 *	@param	replace	替换后
	 *	@return	
	 */
	public static function replace(str:String, find:String, replace:String):String {
		return str.split(find).join(replace);
	}
	/**
	 *	htmlEncode
	 *	
	 *	@usage	
	 *	@param	str	
	 *	@return	
	 */
	public static function htmlEncode(str:String):String {
		var s = str;
		s = replace(s, "&", "&amp;");
		s = replace(s, "<", "&lt;");
		s = replace(s, ">", "&gt;");
		s = replace(s, "\u0022", "&quot;");
		s = replace(s, "  ", "&nbsp; ");
		s = replace(s, "\u0009", "&nbsp;&nbsp;&nbsp;&nbsp;");
		return s;
	}
	/**
	 *	htmlDecode
	 *	
	 *	@usage	
	 *	@param	str	
	 *	@return	
	 */
	public static function htmlDecode(str:String):String {
		var s = str;
		s = replace(s, "&lt;", "<");
		s = replace(s, "&gt;", ">");
		s = replace(s, "&quot;", "\u0022");
		s = replace(s, "&nbsp;&nbsp;&nbsp;&nbsp;", "\u0009");
		s = replace(s, "&nbsp;", " ");
		s = replace(s, "&", "&amp;");
		return s;
	}
	/**
	 *	去左端空白
	 *	
	 *	@usage	
	 *	@param	str	
	 *	@return	
	 */
	public static function lTrim(str:String):String {
		var s:String = str;
		var len:Number = s.length;
		var c:String;
		var i:Number = -1;
		while (++i<len) {
			c = s.substr(i, 1);
			if (c != " " && c != "\n" && c != "\f" && c != "\r" && c != "\t") {
				return s.substr(i, len-i);
			}
		}
	}
	/**
	 *	去右端空白
	 *	
	 *	@usage	
	 *	@param	str	
	 *	@return	
	 */
	public static function rTrim(str:String):String {
		var s:String = str;
		var len:Number = s.length;
		var c:String;
		var i:Number = len;
		while (--i>0) {
			c = s.substr(i, 1);
			if (c != " " && c != "\n" && c != "\f" && c != "\r" && c != "\t") {
				return s.substr(0, ++i);
			}
		}
	}
	/**
	 *	去两端空白
	 *	
	 *	@usage	
	 *	@param	str	
	 *	@return	
	 */
	public static function trim(str:String):String {
		return lTrim(rTrim(str));
	}
}
